$(function () {

    var articleType = [];

    var isTop = [
        {val: 0, text: '否'},
        {val: 1, text: '是'}
    ];

    function articleTbody(list) {
        var str = '';
        for (var i = 0; i < list.length; i++) {
            str += '<tr data-id=' + list[i].id + '>' +
                '<td>' + list[i].title + '</td>' +
                '<td>' + articleTypeList(list[i].type) + '</td>' +
                '<td>' + list[i].isTop + '</td>' +
                '<td>' + new Date(list[i].createTime).format('yyyy-MM-dd hh:ss:mm') + '</td>' +
                '<td>' + list[i].createUser + '</td>' +
                '<td><a class="btn btn-primary btn-xs" href="#/article-page/' + list[i].id + '">编辑文章內容 </a> <a class="btn btn-primary btn-xs delete-btn"> 删除文章</a></td>' +
                '</tr>'
        }

        return str
    }

    function articleTypeList(type) {
        for (var i = 0; i < articleType.length; i++) {
            var _type = articleType[i];
            if (_type.val == type)
                return _type.text;
        }
        return '未设置';
    }

    function layerArticle(opts) {
        return '<div style="padding: 10px 0"> ' +
            '<div class="form-group">' +
            '<label class="col-sm-3 control-label">文章名称:<span class="asterisk">*</span></label>' +
            '<div class="col-sm-7">' +
            ' <input  type="text" class="form-control input-sm article-title"  placeholder="文章名称" value="' + opts.title + '" />' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-3 control-label">所属类型:<span class="asterisk">*</span></label>' +
            '<div class="col-sm-7">' +
            '<select class="form-control article-type" >' + (function () {
                var str = "";
                for (var i = 0; i < articleType.length; i++) {
                    str += '<option  value="' + articleType[i].val + '">' + articleType[i].text + '</option>';
                }

                return str

            })() + '</select>' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-3 control-label">是否置顶:<span class="asterisk">*</span></label>' +
            '<div class="col-sm-7">' +
            '<select class="form-control article-isTop" >' + (function () {
                var str = "";
                for (var i = 0; i < isTop.length; i++) {
                    str += '<option  value="' + isTop[i].val + '">' + isTop[i].text + '</option>';
                }

                return str

            })() + '</select>' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-3 control-label">创建人:<span class="asterisk">*</span></label>' +
            '<div class="col-sm-7">' +
            ' <input  type="text" class="form-control input-sm article-createUser"  placeholder="创建人" value="' + opts.createUser + '" />' +
            '</div>' +
            '</div>' +
            '</div>'
    }


    function queryDatum() {
        pageInit({
            pageNum: 1,
            pageSize: 10,
            token: window.localStorage.getItem('token'),
            title: $("#article-title").val() || "",
            ownerName: $("#article-creator").val() || "",
        });
    }

    function resetDatum() {
        pageInit({
            pageNum: 1,
            pageSize: 10,
            token: window.localStorage.getItem('token')
        });
    }


    function pageInit(requestData) {
        var pageInit_layer = layer.load(0);
        $.restPost('/article/doPageQuery', requestData).done(function (msg, data) {

            layer.close(pageInit_layer);

            $("#article-table tbody").html(articleTbody(data.list))

            $.restPost('/type/doQueryType').done(function (mess, types) {
                for(var i = 0 ; i < types.list.length;i++){
                    articleType.push(types.list[i]);
                }
                $("#article-add").off("click").on("click", function () {
                    $.rootOpenDialog({
                        title: "新增文章",
                        btn: ["提交"],
                        content: layerArticle({
                            title: "",
                            type: articleType,
                            sort: "",
                            createUser: window.localStorage.getItem("userName")
                        }),
                        yes: function (index, lay) {
                            var $lay = $(lay)
                            // console.log($lay.find(".room-title").val())
                            var addRoom_layer1 = layer.load(0);
                            $.restPost('/article/doInsert', {
                                token: window.localStorage.getItem('token'),
                                title: $lay.find(".article-title").val(),
                                isTop: $lay.find(".article-isTop").val(),
                                createUser: $lay.find('.article-createUser').val()
                            }).done(function (msg, data) {
                                layer.close(addRoom_layer1)
                                layer.close(index)
                                $.rootSuccessDialog(msg, 2000)
                                pageInit(requestData);
                            }).fail(function (msg) {
                                layer.close(addRoom_layer1)
                                $.rootErrorDialog(msg, 2000)
                            })

                        }
                    })


                    $("#queryDatum").off("click").on("click", function () {
                        queryDatum()
                    });

                    $("#resetDatum").off("click").on("click", function () {
                        resetDatum()
                    });

                    laypage({
                        cont: 'page', //容器。
                        pages: data.pages, //通过后台拿到的总页数
                        curr: requestData.pageNum || 1, //当前页
                        skip: true, //是否开启跳页
                        skin: 'molv', //皮肤,
                        jump: function (obj, first) { //触发分页后的回调
                            requestData.pageNum = obj.curr;
                            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                                pageInit(requestData);
                            }
                        }
                    });
                });
            });
        })
    }

    pageInit({
        pageNum: 1,
        pageSize: 10,
        token: window.localStorage.getItem('token')
    })
})

